package com.yc.sakuralxm.uestcscanning;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.yc.sakuralxm.uestcscanning.fragment.ScanFragment;
import com.yc.sakuralxm.uestcscanning.tools.L;


//@RuntimePermissions
public class MainTapActivity extends BaseFragmentActivity {
    // 定义FragmentTabHost对象
    private FragmentTabHost mTabHost;

    // 定义一个布局
    private LayoutInflater
            layoutInflater;



    public enum MainType {
        scan(ScanFragment.class, R.mipmap.ic_launcher, "车牌扫描"),
        scan1(ScanFragment.class, R.mipmap.ic_launcher, "车牌扫描1");
        //        home(FragmentHome.class, R.drawable.tab_home_btn, "直播"),
//        news(FragmentNew.class, R.drawable.icon_news_dy, "新闻动态"),
//        rivrTai(FragmentRiverTai.class, R.drawable.icon_work, "河长工作台"),
//        home(FragmentRiverCenter.class, R.drawable.icon_login_user, "河长中心");
//        focus(FocusLiveFragment.class, R.drawable.icon_head, "关注"),
//        jubao(FragmentJubao.class, R.drawable.icon_login_user, "举报公示"),
//        jubao2(FragmentJubao.class, R.drawable.icon_login_user, "举报公示");
//        my(FragmentMy.class,R.drawable., "巡河记录");

        //        jubao(FragmentJubao.class,R)
        // Fragment界面
        private Class<?> class1;
        // 按钮图片
        private int drawableId;
        // Tab选项卡的文字
        private String name;

        MainType(Class<?> class1, int drawableId, String name) {
            this.class1 = class1;
            this.drawableId = drawableId;
            this.name = name;
        }

        public Class<?> getClass1() {
            return class1;
        }

        public int getDrawableId() {
            return drawableId;
        }

        public String getName() {
            return name;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent  = new Intent(".MainActivity");
            startActivity(intent);
//            closeCamera();
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
    public void onCreate(Bundle savedInstanceState) {
        L.INSTANCE.i(getClass().getSimpleName() + " onCreate");
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            setTranslucentStatus(true);
//            SystemBarTintManager tintManager = new SystemBarTintManager(this);
//            tintManager.setStatusBarTintEnabled(true);
//            tintManager.setStatusBarTintResource(R.color.color_home_bg);//通知栏所需颜色
//        }
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        setContentView(R.layout.activity_main_tap);
        initView();
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void initLeftMenu() {
        // TODO Auto-generated method stub
    }

    /**
     * 初始化组件
     */
    private void initView() {
        // 实例化布局对象
        layoutInflater = LayoutInflater.from(this);
        // 实例化TabHost对象，得到TabHost
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        for (MainType item : MainType.values()) {
            // 为每一个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(item.getName()).setIndicator(getTabItemView(item));
            // 将Tab按钮添加进Tab选项卡中
            mTabHost.addTab(tabSpec, item.getClass1(), null);
//			i++;
        }
        //去掉分隔线
        mTabHost.getTabWidget().setDividerDrawable(null);
    }

    /**
     * 给Tab按钮设置图标和文字
     */
    private View getTabItemView(MainType item) {
        View view = layoutInflater.inflate(R.layout.tab_item_view, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
        TextView textView = (TextView) view.findViewById(R.id.textview);
        if (item.getDrawableId() == 0) {
            imageView.setAlpha(0.0f);
            textView.setAlpha(0.0f);
        } else {
            imageView.setImageResource(item.getDrawableId());

            textView.setText(item.getName());
        }
        return view;
    }
    /**
     * 更新文字颜色。
     *
     * @param
     */



    private long lastTime;

    @Override
    public void onBackPressed() {
        long curTime = System.currentTimeMillis();
        if (curTime - lastTime > 2000) {//大于两秒不能退出
            lastTime = curTime;
            AppContext.Companion.getApp().showToastCenterShort("再点击一次返回可退出程序");
            return;
        }
        super.onBackPressed();
    }

    public static final String[] NAMES = {"icon_login_bg.png", "icon_reg_bg.png", "icon_frag_my_bg.png", "icon_bg_live_authority.png", "icon_member_bg.png"};




    // 添加此处目的是针对后台APP通过uri scheme唤起的情况，
    // 注意：即使不区分用户是否登录也需要添加此设置，也可以添加到基类中
    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
    }
//    public void showLeft(View view) {
//        getSlidingMenu().toggle();
//    }
//    @TargetApi(19)
//    private void setTranslucentStatus(boolean on) {
//        Window win = getWindow();
//        WindowManager.LayoutParams winParams = win.getAttributes();
//        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
//        if (on) {
//            winParams.flags |= bits;
//        } else {
//            winParams.flags &= ~bits;
//        }
//        win.setAttributes(winParams);
//    }


//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        L.i("MainTabActivity onActivityResult");
//        L.i(String.format(Locale.CHINA, "RESULT_OK = %d,requestCode = %d,resultCode = %d", RESULT_OK, requestCode, resultCode));
//        if(takeOrSelectPhoto(requestCode, resultCode, data)){
//            return;
//        }
//        if (curFragment != null) {
//            curFragment.onActivityResult(requestCode, resultCode, data);
//            return;
//        }
//        super.onActivityResult(requestCode, resultCode, data);
//    }
//    /**
//     * 点击修改头像
//     *
//     */
//    @NeedsPermission(android.Manifest.permission.CAMERA)//在需要获取权限的地方注释
//    void changeHead() {
//        AppContext.getApp().showToastCenterLong("获取权限");
//    }
//
//    @OnShowRationale(android.Manifest.permission.CAMERA)//提示用户为什么需要此权限
//    void showWhy(final PermissionRequest request) {
//        new AlertDialog.Builder(this)
//                .setMessage("权限测试")
//                .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        request.proceed();//再次执行请求
//                    }
//                })
//                .show();
//    }
//    @OnPermissionDenied(android.Manifest.permission.CAMERA)//一旦用户拒绝了
//    void denied() {
//        AppContext.getApp().showToastCenterLong("真的不给权限吗");
//    }
//    @OnNeverAskAgain(android.Manifest.permission.CAMERA)//用户选择的不再询问
//    void notAsk() {
//        AppContext.getApp().showToastCenterLong("好的不问了");
//    }
//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//        MainTabActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
//    }
}
